package com.atguigu.gmall.payment.controller;

import com.alipay.api.AlipayApiException;
import com.atguigu.gmall.common.bean.UserInfo;
import com.atguigu.gmall.common.exception.OrderException;
import com.atguigu.gmall.oms.entity.OrderEntity;
import com.atguigu.gmall.payment.config.AlipayTemplate;
import com.atguigu.gmall.payment.interceptor.LoginInterceptor;
import com.atguigu.gmall.payment.service.PaymentService;
import com.atguigu.gmall.payment.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private AlipayTemplate alipayTemplate;


    @GetMapping("pay.html")
    public String toPay(@RequestParam("orderToken")String orderToken, Model model){

        OrderEntity orderEntity = this.paymentService.queryOrderByOrderToken(orderToken);

        // 判断订单是否存在
        if (orderEntity == null){
            throw new OrderException("您支付的订单不存在！");
        }

        // 判断当前订单是否属于登录用户
        UserInfo userInfo = LoginInterceptor.getUserInfo();
        Long userId = userInfo.getUserId();
        if (orderEntity.getUserId() != userId){
            throw new OrderException("这个订单不属于你！");
        }

        // 判断订单状态 待付款
        if (orderEntity.getStatus() != 0){
            throw new OrderException("这个订单不能支付！");
        }

        model.addAttribute("orderEntity",orderEntity);
        return "pay";
    }

    @GetMapping("alipay.html")
    @ResponseBody // 以其他视图形式展示
    public String alipay(@RequestParam("orderToken")String orderToken){

        OrderEntity orderEntity = this.paymentService.queryOrderByOrderToken(orderToken);

        // 判断订单是否存在
        if (orderEntity == null){
            throw new OrderException("您支付的订单不存在！");
        }

        // 判断当前订单是否属于登录用户
        UserInfo userInfo = LoginInterceptor.getUserInfo();
        Long userId = userInfo.getUserId();
        if (orderEntity.getUserId() != userId){
            throw new OrderException("这个订单不属于你！");
        }

        // 判断订单状态 待付款
        if (orderEntity.getStatus() != 0){
            throw new OrderException("这个订单不能支付！");
        }

        try {
            // 调用接口，打开一个支付页面
            PayVo payVo = new PayVo();
            payVo.setOut_trade_no(orderEntity.getOrderSn());
            // 建议写0.01
            payVo.setTotal_amount("0.01");
            payVo.setSubject("谷粒商城支付平台");

            // 记录对账信息
            Long payId = this.paymentService.savePaymentInto(payVo);

            payVo.setPassback_params(payId.toString());

            String form = this.alipayTemplate.pay(payVo);

            return form;
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        throw new OrderException("支付失败！");
    }

    /**
     * 支付宝同步回调路径
     */
    @GetMapping("pay/success")
    public String paySuccess(){

        return "paysuccess";
    }

    /**
     * 支付宝异步回调路径
     */
    @PostMapping("pay/ok")
    @ResponseBody
    public Object payOk(){

        return null;
    }

}
